package OptimalAlgorithm.Hash;

import java.util.HashMap;

public class CheckPermutation {
/*    public boolean checkPermutation(String s1, String s2) {
        if(s1.length() != s2.length()){
            return false;
        }
        HashMap<Character,Integer> hash1 = new HashMap<>();
        HashMap<Character,Integer> hash2 = new HashMap<>();
        for (int i = 0; i < s1.length(); i++) {
            hash1.put(s1.charAt(i),hash1.getOrDefault(s1.charAt(i),0) + 1);
        }
        for (int i = 0; i < s1.length(); i++) {
            hash2.put(s2.charAt(i),hash2.getOrDefault(s2.charAt(i),0) + 1);
        }
        return hash1.equals(hash2);//比较两个hash表是否内容完全相同
    }*/
    //用数组来模拟hash表
    public boolean checkPermutation(String s1, String s2) {
        if(s1.length() != s2.length()){
            return false;
        }
        int[] hash1 = new int[26];
        int[] hash2 = new int[26];
        for (int i = 0; i < s1.length(); i++) {
            char x = s1.charAt(i);
            hash1[x - 97]++;
        }
        for (int i = 0; i < s2.length(); i++) {
            char x = s2.charAt(i);
            hash2[x - 97]++;
        }
        for (int i = 0; i < 26; i++) {
            if(hash1[i] != hash2[i]){
                return false;
            }
        }
        return true;
    }
}
